from flask import Blueprint

from service.CommentService import assemble_comment_contact, get_contact_comment, save_comment
from service.FriendLinkService import save_friend_link, asseble_friend_link
from settings import Setting
from util.EmailUtil import send_email

api_contact = Blueprint('api_contact', __name__)
api_contact.url_prefix = '/api/contact'


@api_contact.route('/friend_link', methods=['POST'])
def friend_link():
    friend = asseble_friend_link()
    receivers = friend.email
    receivers_name = friend.name
    save_friend_link(friend)
    user = Setting.email
    auth_code = Setting.auth_code
    sender = Setting.email
    subject = "Hi,{}您的友链已经提交".format(receivers_name)
    content = '<h1>感谢您提交友链申请我们将会尽快通过</h1>'
    send_email(user, auth_code, sender, receivers, subject, content)
    return {"code": 200, "msg": "success", "data": []}


@api_contact.route("/comment", methods=['POST'])
def add_concat_comment():
    comment = assemble_comment_contact()
    save_comment(comment)
    data = '感谢你的评论'
    return {"code": 200, "msg": "success", "data": data}


@api_contact.route("/comment", methods=['GET'])
def get_concat_comment():
    comments = get_contact_comment()
    return {"code": 200, "msg": "success", "data": comments}
